package com.ship.boats.controller;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.ship.boats.domain.SpBoastPortMessage;
import com.ship.boats.domain.SpBoatsAisMessage;
import com.ship.boats.domain.SpBoatsMmsiChange;
import com.ship.boats.domain.SpBoatsSisterManage;
import com.ship.boats.domain.dto.QueryBoatsPortRq;
import com.ship.boats.domain.vo.*;
import com.ship.boats.service.SpBoastPortMessageService;
import com.ship.boats.service.SpBoatsAisMessageService;
import com.ship.boats.service.SpBoatsMmsiChangeService;
import com.ship.boats.service.SpBoatsSisterManageService;
import com.ship.boats.util.ExportDownloadUtil;
import com.ship.common.core.domain.R;
import com.ship.common.core.web.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * <b>BoatsAISMessageController</b>
 *
 * @description: BoatsAISMessageController <br>
 * @date: 2024/4/7 15:05 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */
@Api(tags = "船舶ais信息管理模块")
@RestController
public class BoatsAISMessageController {
    private static final Logger logger = LoggerFactory.getLogger(BoatsAISMessageController.class);

    @Autowired
    private SpBoatsAisMessageService spBoatsAisMessageService;

    @Autowired
    private SpBoastPortMessageService spBoastPortMessageService;

    @Autowired
    private SpBoatsMmsiChangeService spBoatsMmsiChangeService;

    @Autowired
    private ExportDownloadUtil exportDownloadUtil;

    @ApiOperation("根据imo号查询AIS信息")
    @GetMapping("/query/ais_message/byImo")
    public JsonResult queryAISMessageByImo(@RequestParam String imo){
        QueryBoatsAisMessageInfo spBoatsAisMessage = spBoatsAisMessageService.queryAISMessageByImo(imo);
        logger.info("根据imo:"+imo+" 查询AIS信息返回结果:{}"+ JSONUtil.parse(spBoatsAisMessage));
        return JsonResult.success(spBoatsAisMessage);
    }

    @ApiOperation("根据imo好查询船舶历史挂靠港记录")
    @PostMapping("/query/boats_port/byImo")
    public JsonResult queryBoatsPortByImo(@RequestBody QueryBoatsPortRq rq){
        logger.info("查询历史船舶历史挂靠港记录入参:{}"+JSONUtil.parse(rq));
        PageInfo<QueryBoastPortMessageInfo> boatsPortList = spBoastPortMessageService.queryBoatsPortByImo(rq);
        logger.info("查询历史船舶历史挂靠港记录返回结果:{}"+JSONUtil.parse(boatsPortList));
        return JsonResult.success(boatsPortList);
    }

    @ApiOperation("根据imo号查询MMSI变更记录")
    @GetMapping("/query/boats_mmsi/byImo")
    public JsonResult queryBoatsMmsiByImo(@RequestParam String imo){
        List<SpBoatsMmsiChange> boatsMmsiChanges = spBoatsMmsiChangeService.queryBoatsMmsiByImo(imo);
        logger.info("根据imo="+imo+"查询mmsi变更记录返回结果:{}"+JSONUtil.parse(boatsMmsiChanges));
        return JsonResult.success(boatsMmsiChanges);
    }

    @ApiOperation("查询船舶性能分析")
    @GetMapping("/query/boatsPerformanceAnalysis")
    public JsonResult queryBoatsPerformanceAnalysis(@RequestParam String imo,@RequestParam String startDate,@RequestParam String endDate){
        List<BoatsPerformanceAnalysisInfo> boatsList = spBoatsAisMessageService.queryBoatsPerformanceAnalysis(imo,startDate,endDate);
        return JsonResult.success(boatsList);
    }

    @ApiOperation("查询所有姊妹船列表")
    @GetMapping("/query/boatsSister/list")
    public JsonResult queryBoatsSisterList(@RequestParam String imo){
        List<QueryBoatsSisterInfo> boatsSisterList = spBoatsAisMessageService.queryBoatsSisterList(imo);
        logger.info("根据imo="+imo+"查询姊妹船列表返回结果:{}"+JSONUtil.parse(boatsSisterList));
        return JsonResult.success(boatsSisterList);
    }

    @ApiOperation("根据imo号导出所有姊妹船列表")
    @GetMapping("/export/boatsSister")
    public JsonResult exportBoatsSister(@RequestParam String imo){
        List<QueryBoatsSisterInfo> boatsSisterList = spBoatsAisMessageService.queryBoatsSisterList(imo);
        String shipName = spBoatsAisMessageService.querySisterBoateName(imo);
        logger.info("根据imo="+imo+"查询姊妹船列表返回结果:{}"+JSONUtil.parse(boatsSisterList));
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("shipName");
        tableNameList.add("imo");
        tableNameList.add("mmsi");
        tableNameList.add("deadWeight");
        tableNameList.add("netTonnage");
        tableNameList.add("propulsionType");
        tableNameList.add("yearOfBuild");
        tableNameList.add("dateOfBuild");
        tableNameList.add("shipBuilder");
        tableNameList.add("countryOfBuild");
        tableNameList.add("registeredOwner");
        tableNameList.add("shipManager");
        tableNameList.add("operator");
        tableNameList.add("groupBeneficialOwner");
        Field[] fields = QueryBoatsSisterInfo.class.getDeclaredFields();
        String url = exportDownloadUtil.pageExport(boatsSisterList,fields,tableNameList, shipName+"姊妹船");
        return JsonResult.success(url);
    }
}
